package com.example.gkcircle.guide;


import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.gkcircle.R;
import com.example.gkcircle.activity.LoginActivity;
import com.example.gkcircle.activity.RegisterActivity;
import com.example.gkcircle.utils.ActivityUtil;

/**
 * Created by Jehu on 16/4/11.
 */
public class GuideViewPagerAdapter extends PagerAdapter {

    private List<View> views;
    private Activity activity;

    public GuideViewPagerAdapter(List<View> views, Activity activity) {
        this.views = views;
        this.activity = activity;
    }

    // 销毁arg1位置的界面
    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView(views.get(arg1));
    }

    @Override
    public void finishUpdate(View arg0) {
    }


    // 获得当前界面数
    @Override
    public int getCount() {
        if (views != null) {
            return views.size();
        }
        return 0;
    }

    // 初始化arg1位置的界面
    @Override
    public Object instantiateItem(final View arg0, int arg1) {
        ((ViewPager) arg0).addView(views.get(arg1), 0);
        if (arg1 == views.size() - 1) {
            Button loginBtn=(Button)arg0.findViewById(R.id.guide_login_btn);
            Button regiterBtn=(Button)arg0.findViewById(R.id.guide_register_btn);
            loginBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.overridePendingTransition(0, R.anim.zoom_in);
                    Intent intent =new Intent(activity,LoginActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putInt("guide",3);
                    intent.putExtras(bundle);
                    activity.startActivityForResult(intent, 0);
                }
            });
            regiterBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.overridePendingTransition(0,R.anim.zoom_in);
                    Intent intent =new Intent(activity,RegisterActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putInt("flag", 0);
                    intent.putExtras(bundle);
                    activity.startActivity(intent);
                }
            });
        }
        return views.get(arg1);
    }


    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return (arg0 == arg1);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View arg0) {
    }

}
